package com.living.player.ui.recently.album;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.R;
import com.living.player.db.RecentlyManger;
import com.living.player.ui.recently.adapter.RecentlyAlbumAdapter;
import com.living.player.ui.recently.audio.RecentlyAudioBatchActivity;
import com.md.basedpc.base.BaseFragment;
import com.md.livingwidget.emptyview.PHEmptyViewUI;
import com.md.livingwidget.toast.PHToast;
import com.md.livingwidget.toast.ToastUtil;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecentlyAlbumFragment extends BaseFragment implements OnItemClickListener, View.OnClickListener {
    public final static String ALBUM_ID = "ALBUM_ID";
    View layout_recently_album_edite;
    TextView tv_recently_album_count;
    private RecentlyAlbumAdapter taskItemAdapter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_player_recently_album;
    }

    /**
     * 移除操作
     *
     * @param t
     */
    public void remove(Audio t) {
        initData();
        if (taskItemAdapter.getData().size() == 0) {
            getActivity().finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        layout_recently_album_edite = view.findViewById(R.id.layout_recently_album_edite);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        tv_recently_album_count = view.findViewById(R.id.tv_recently_album_count);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        view.findViewById(R.id.iv_recently_album_edite).setOnClickListener(this);

        taskItemAdapter = new RecentlyAlbumAdapter();
        recyclerView.setAdapter(taskItemAdapter);

        taskItemAdapter.setOnItemClickListener(this);

        PHEmptyViewUI phEmptyViewUI = new PHEmptyViewUI(getActivity());
        taskItemAdapter.setEmptyView(phEmptyViewUI);

        initData();

        return view;
    }

    private void initData() {
        List<Album> audioList = RecentlyManger.getInstance().getAllRecentAlbums();
        taskItemAdapter.setNewInstance(audioList);
        tv_recently_album_count.setText(audioList.size() + "张专辑");
        layout_recently_album_edite.setVisibility(audioList.size() == 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.iv_recently_album_edite) {
            getActivity().startActivityForResult(new Intent(getActivity(), RecentlyAlbumBatchActivity.class), 123);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Album album = taskItemAdapter.getItem(position);
        if (album != null && !TextUtils.isEmpty(album.getId())) {
            int albumId = -1;
            try {
                albumId = Integer.parseInt(album.getId());
                ARouter.getInstance().build("/album2/album").withInt("ALBUM_ALBUM_ID", albumId).navigation();
            } catch (Exception e) {
                PHToast.showSingleToast(getActivity(), "专辑ID参数异常");
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            initData();
        }
    }
}
